package com.zaker.touchtv.data;

import android.util.Log;

import com.zaker.touchtv.utils.Constants;
import com.zaker.touchtv.utils.IOUtils;

import java.io.File;
import java.util.concurrent.TimeUnit;

import okhttp3.Cache;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

import static com.zaker.touchtv.utils.Constants.LOG_TAG;

public class AppApiClient {

  private final static AppApiClient INSTANCE = new AppApiClient();

  private final Retrofit mAppRetrofit;

  private final static String APP_BASE_URL = "http://121.9.213.58/iphone.myzaker.com/";

  private AppLiveService mLiveService;

  private AppApiClient() {

    final File dirFile = new File(IOUtils.getDirectory(LOG_TAG, Constants.APP_CONTEXT));
    boolean isSuccess = dirFile.mkdirs();

    Log.i(LOG_TAG, " mkdirs: " + dirFile.toString() + " isSuccess: " + isSuccess);

    final int cacheSize = 10 * 1024 * 1024;//10M
    final Cache cache = new Cache(dirFile, cacheSize);

    final OkHttpClient okHttpClient = new OkHttpClient.Builder()
//        .addInterceptor(new Interceptor() {
//      @Override
//      public Response intercept(Chain chain) throws IOException {
//
//        Request request = chain.request();
//
//        request = request.newBuilder()
//            .cacheControl(CacheControl.FORCE_CACHE)
//            .build();
//
//        Response response = chain.proceed(request);
//
//        Log.i(LOG_TAG, "CacheControl.FORCE_CACHE: " + response.code());
//
//        if (!response.isSuccessful()) {
//          request = request.newBuilder().cacheControl(CacheControl.FORCE_NETWORK).build();
//          response = chain.proceed(request);
//          Log.i(LOG_TAG, "CacheControl.FORCE_NETWORK: " + response.code());
//        }
//        return response;
//      }
//    }).addNetworkInterceptor(new Interceptor() {
//      @Override
//      public Response intercept(Chain chain) throws IOException {
//
//        Request request = chain.request();
//        Response response = chain.proceed(request);
//
//        Log.i(LOG_TAG, "addNetworkInterceptor response.code(): " + response.code() + " isMain: "
//            + (Looper.getMainLooper() == Looper.myLooper()));
//
//        Log.i(LOG_TAG, "addNetworkInterceptor response 1headers: "
//            + response.headers().toString());
//
//        final CacheControl cacheControl = new CacheControl.Builder()
//            .maxAge(2, TimeUnit.DAYS)
//            .build();
//
//        response = response.newBuilder()
//            .header("Cache-Control", cacheControl.toString())//一周
//            .removeHeader("Pragma").build();
//
//        Log.i(LOG_TAG, "addNetworkInterceptor response 2headers: "
//            + response.headers().toString());
//
//        return response;
//      }
//    })
        .connectTimeout(10, TimeUnit.SECONDS).
        writeTimeout(10, TimeUnit.SECONDS).
        readTimeout(12, TimeUnit.SECONDS).
        cache(cache).
        build();

    mAppRetrofit = new Retrofit.Builder().baseUrl(APP_BASE_URL)
        .addConverterFactory(GsonConverterFactory.create())
        .client(okHttpClient)
        .build();
  }

  public static AppLiveService getLiveService() {
    return INSTANCE.createLiveService();
  }

  private AppLiveService createLiveService() {
    if (mLiveService == null) {
      mLiveService = mAppRetrofit.create(AppLiveService.class);
    }
    return mLiveService;
  }
}
